
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_uxin/Tools/config/md5Tool.dart';
import 'package:flutter_uxin/Tools/Define/Device.dart';



class  commonTool {


  //获取缓存数据
  static Future<String> getPreferencesValue(String key) async{

    SharedPreferences prefs = await SharedPreferences.getInstance();

    String value = prefs.getString('$key');

    return   value;
  }

  //清除缓存数据
  static Future<bool> clearPreferencesValue() async{
     SharedPreferences prefs = await SharedPreferences.getInstance();
     bool  isSucess = await prefs.clear();

     return  isSucess;
  }

  //获取通用请求数据
  static Future  <Map>   getCommonPostForm() async{



    String device_type = Device.getDevice_type();
    String deviceID = await Device.deviceID();
    String currentTime = Device.currentTimeMillis().toString();


    SharedPreferences prefs = await SharedPreferences.getInstance();
    String user_name = prefs.getString('profile_name');
    String user_id = prefs.getString('profile_id');
    String school_id = prefs.getString('profile_school_id');



    Map  <String, String>  postMap = new Map();

    postMap['user_name'] = '$user_name';
    postMap['time'] = '$currentTime';
    postMap['device_type'] = '$device_type';
    postMap['device_identifier'] = '$deviceID';
    postMap['user_id'] = '$user_id';
    postMap['school_id'] = '$school_id';

     print('postMap--------------$postMap');
    return  postMap;
  }

  static changeValueToBool(var value){
    bool boolValue = false;
    if(value == null){
      boolValue = false;
    } else if (value is String) {
    if (value == '1') {
      boolValue = true;
    } else {
      boolValue = false;
    }
  }else if (value is int){
    if(value == 1){
      boolValue = true;
    }else{
      boolValue = false;
    }
  }else if (value is bool){
    boolValue = value;
  } else {
    boolValue = false;
  }

  return boolValue;
}


  //获取用户头像
  static Future  <String>  getHeadPortrait(String  extendUrl ) async{


    if(extendUrl.startsWith('/cache') == true ){
      String download_url = await commonTool.getPreferencesValue('areaDNS_download_url');

      return  download_url + extendUrl;

    }else if(extendUrl.startsWith('/upload') == true ){
      String vfs_url = await commonTool.getPreferencesValue('areaDNS_vfs_url');
      return  vfs_url + '/tms' +extendUrl;
    }else if(extendUrl.startsWith('file') == true ){

      return extendUrl;
    }else{
      return extendUrl;
    }
  }

}